package com.zgm.ch06.aop.aspectJ.pointCut._2_1;

import java.util.Arrays;

import javax.annotation.PostConstruct;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.util.Assert;

@Aspect
public class LogginAspectPC {

	private String beforeMessage;
	private String afterMessage;
	
	@Pointcut("execution(* com.zgm.ch06.aop.aspectJ.annotation._1.TestBean.*(..))")  /////
	private void testBeanExecution(){}////////
	
//	@Around("execution(* com.zgm.ch06.aop.aspectJ.annotation._1.TestBean.*(..))")
	@Around("testBeanExecution()")/////////
	public Object log(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println(String.format(this.beforeMessage, pjp.getSignature().getName(),
							Arrays.toString(pjp.getArgs())));
		Object ret = pjp.proceed();
		System.out.println(String.format(this.afterMessage, pjp.getSignature().getName(),
							Arrays.toString(pjp.getArgs())));
		
		return ret;
	}
	
	
	
	@PostConstruct //没有起作用
	public void initialize() {
		Assert.notNull(this.beforeMessage, "beforeMessage必须设置值");
		Assert.notNull(this.afterMessage, "afterMessage必须设置值");
		
	}
	
	public void setAfterMessage(String afterMessage) {
		this.afterMessage = afterMessage;
	}

	public void setBeforeMessage(String beforeMessage) {
		this.beforeMessage = beforeMessage;
	}
}
